package cn.zzzhy.core.descriptor;

import com.google.common.base.CaseFormat;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * author:zzzhy
 * since :15/11/9.
 */
public class Column {

    /**
     * 用于获取数据库类型对应的java类型
     */
    private static Map<String, Class<?>> typeMapping = Maps.newHashMap();

    static {
        typeMapping.put("varchar", String.class);
        typeMapping.put("enum", String.class);
        typeMapping.put("bigint", Long.class);
        typeMapping.put("long", Long.class);
        typeMapping.put("integer", Integer.class);
        typeMapping.put("float", Float.class);
        typeMapping.put("double", Double.class);
        typeMapping.put("int", Integer.class);
        typeMapping.put("timestamp", Date.class);
        typeMapping.put("datetime", Date.class);
        typeMapping.put("boolean", boolean.class);
        typeMapping.put("bool", boolean.class);
        typeMapping.put("decimal", BigDecimal.class);
    }

    /**
     * 用于实体类注解验证
     */
//    private static Map<String, Class<?>> validationRules = Maps.newHashMap();
//
//    static {
//        validationRules.put("pattern", Pattern.class);
//        validationRules.put("email", Email.class);
//        validationRules.put("url", URL.class);
//        validationRules.put("length", Length.class);
//        validationRules.put("notNull", NotNull.class);
//        validationRules.put("notEmpty", NotEmpty.class);
//        validationRules.put("min", Min.class);
//        validationRules.put("max", Max.class);
//    }

    public static class Validation {
        public final Class<?> clazz;
        private final String annotation;

        public Validation(Class<?> clazz, String annotation) {
            this.clazz = clazz;
            this.annotation = annotation;
        }

        public String getAnnotation() {
            return annotation;
        }
    }

    public String columnName;
    public boolean primary;
    public String dataType;
    public String columnType;
    public int size;
    public boolean nullable;
    private Object defaultValue;

    private List<String> enumValues = Lists.newArrayList();
    private List<Validation> validations = Lists.newArrayList();

    public String getFieldName() {
        return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL,columnName);
    }

}
